package pers.qianyu.diytomcat.catalina;

import cn.hutool.log.*;
import pers.qianyu.diytomcat.http.*;
import pers.qianyu.diytomcat.util.*;

import java.io.*;
import java.net.*;

/**
 * @author mizzle rain
 * @date 2021-01-08 11:52
 */
public class Connector implements Runnable {
    private int port;
    private Service service;

    private String compression;
    private int compressionMinSize;
    private String noCompressionUserAgents;
    private String compressableMimeType;

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            while (true) {
                Socket socket = serverSocket.accept();
                ThreadPoolUtil.run(() -> {
                    try {
                        Request request = new Request(socket, this);
                        Response response = new Response();
                        HttpProcessor httpProcessor = new HttpProcessor();
                        httpProcessor.execute(socket, request, response);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                });
            }
        } catch (Exception e) {
            LogFactory.get().error(e);
        }
    }

    public void init() {
        LogFactory.get().info("Initializing ProtocolHandler [http-bio-{}]", port);
    }

    public void start() {
        LogFactory.get().info("Starting ProtocolHandler [http-bio-{}]", port);
        new Thread(this).start();
    }

    /************** automatic generated *****************/
    public Connector(Service service) {
        this.service = service;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    public String getCompression() {
        return compression;
    }

    public void setCompression(String compression) {
        this.compression = compression;
    }

    public int getCompressionMinSize() {
        return compressionMinSize;
    }

    public void setCompressionMinSize(int compressionMinSize) {
        this.compressionMinSize = compressionMinSize;
    }

    public String getNoCompressionUserAgents() {
        return noCompressionUserAgents;
    }

    public void setNoCompressionUserAgents(String noCompressionUserAgents) {
        this.noCompressionUserAgents = noCompressionUserAgents;
    }

    public String getCompressableMimeType() {
        return compressableMimeType;
    }

    public void setCompressableMimeType(String compressableMimeType) {
        this.compressableMimeType = compressableMimeType;
    }
}
